﻿using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;

using PlanningGIS.ArcGIS;
using PlanningGIS.Util.Log;
using PlanningGIS.Engine.Controls;

namespace PlanningGIS.DataCheck.model
{
    public class GHCheck : Check
    {
        public int Check(IFeatureLayer pFeatLyr)
        {
            IFeatureClass pFeatCls = pFeatLyr.FeatureClass;

            if (HasZ(pFeatCls) == false)
            {
                
                    LogHelper.WriteLog("[" + pFeatLyr.Name + "]图层不是三维矢量数据，检查失败！");
                return 0;
            }

            esriGeometryType pGeoType = pFeatCls.ShapeType;
            if (pGeoType != esriGeometryType.esriGeometryPoint
                && pGeoType != esriGeometryType.esriGeometryPolyline
                && pGeoType != esriGeometryType.esriGeometryPolygon)
            {
                
                    LogHelper.WriteLog("[" + pFeatLyr.Name + "]图层不是图形质量要素数据，检查失败！");
                return 0;
            }

            IFeatureSelection pFeatSel = (IFeatureSelection)pFeatLyr;

            ISelectionSet pSelset;
            IEnumIDs pIDs;

            //记录错误图形
            IGeometryCollection pGeomCln = (IGeometryCollection)new GeometryBag();

            int errCount = 0;
            int count = 0;

            if (showFakePro != null) showFakePro(true);
            
                LogHelper.WriteLog("开始形成选择集。。。");
            pSelset = CursorHelper.GetSelset(pFeatSel, null, "", esriSpatialRelEnum.esriSpatialRelIntersects);
            
                LogHelper.WriteLog("形成选择集完毕。共有" + pSelset.Count + "个要素参与检查。");

            if (showFakePro != null) showFakePro(false);
            if (pSelset.Count == 0) return 0;

            if (setProcessBarMax != null) setProcessBarMax(pSelset.Count);

            pIDs = pSelset.IDs;
            long lID;

            while ((lID = pIDs.Next()) != -1)
            {
                if (IsStop()) break;

                count++;

                bool bErr = false;

                if (setProcessBarCurrent != null) setProcessBarCurrent(count);

                IFeature pFeature = pFeatCls.GetFeature((int)lID);
                IZ pZ;
                switch (pGeoType)
                {
                    case esriGeometryType.esriGeometryPoint:
                        IPoint pPoint = (IPoint)pFeature.Shape;
                        if (pPoint.Z != 0)
                        {
                            bErr = true;
                        }
                        break;
                    case esriGeometryType.esriGeometryPolyline:
                        IPolyline pPolyline = (IPolyline)pFeature.Shape;
                        pZ = (IZ)pPolyline;
                        //modified by sheng 
                        if (Math.Abs(pZ.ZMax) > 0.001 || Math.Abs(pZ.ZMin) > 0.001)
                        //if (pZ.ZMax > 0 && pZ.ZMin < 0)
                        {
                            bErr = true;
                        }
                        break;
                    case esriGeometryType.esriGeometryPolygon:
                        IPolygon pPolygon = (IPolygon)pFeature.Shape;
                        pZ = (IZ)pPolygon;
                        //modified by sheng 
                        if (Math.Abs(pZ.ZMax) > 0.001 || Math.Abs(pZ.ZMin) > 0.001)
                        //if (pZ.ZMax > 0 && pZ.ZMin < 0)
                        {
                            bErr = true;
                        }
                        break;
                }

                if (bErr == true)
                {
                    RecordListHelper.AddRecordItem("高程异常", pFeatLyr, null, pFeature, null, pFeature.Shape, "");
                    pGeomCln.AddGeometry(pFeature.Shape, ref Missing, ref  Missing);
                }
            }

            pFeatSel.Clear();

            if (finishPro != null) finishPro();
            errCount = pGeomCln.GeometryCount;
            DataCheckUtility.ReleaseComObject(pGeomCln);

            
                LogHelper.WriteLog("高程异常结束。共发现" + errCount + "处错误！\n");

            return errCount;
        }

        public static bool HasZ(IFeatureClass pFcls)
        {
            IField pFld = pFcls.Fields.get_Field(pFcls.FindField(pFcls.ShapeFieldName));
            IGeometryDef pGeometryDef = pFld.GeometryDef;
            return pGeometryDef.HasZ;
        }
    }
}
